package com.zretc.support.controller;

import com.zretc.support.entity.Question;
import com.zretc.support.entity.User;
import com.zretc.support.service.QuestionSerive;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("api")
public class QuestionController {
    @Autowired
    private QuestionSerive qs;

    @Autowired
    private AjaxResponse ajaxResponse;

    @Autowired
    private PageData pd;

    Logger logger = LoggerFactory.getLogger(QuestionController.class);

    /**
     * 分页模糊查询问题
     *
     * @param keywords
     * @param startTime
     * @param endTime
     * @param page
     * @param size
     * @return
     */
    @GetMapping("gqbl")
    public PageData<Question> getQ(@RequestParam(value = "keywords", defaultValue = "") String keywords,
                                   @RequestParam(value = "startTime", defaultValue = "") String startTime,
                                   @RequestParam(value = "endTime", defaultValue = "") String endTime,
                                   @RequestParam(value = "page", defaultValue = "0") int page,
                                   @RequestParam(value = "size", defaultValue = "10") int size,
                                   @RequestParam(value = "select", defaultValue = "1") int select) {

        //将参数输出查看一下
        logger.info("keywords:" + keywords + ",select:" + select);

        //设置时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //赋初值
        Date sd = null;
        Date ed = null;
        //若开始的时间为空，则说明没有传值，那就查所有给个2000年的时间为初值
        if (startTime.equals("")) {
            try {
                logger.info("是否执行");
                sd = simpleDateFormat.parse("2000-01-01");
                logger.info("sd:" + sd);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            logger.info("执行");
            try {
                sd = simpleDateFormat.parse(startTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //若结束的时间为空，则说明没有传值，那就查所有给个当前时间的后一天为末值
        if (endTime.equals("")) {
            Calendar c = Calendar.getInstance();
            //   c.add(Calendar.DAY_OF_MONTH, 1);
            ed = c.getTime();
            logger.info("ed:" + ed);
        } else {
            try {
                ed = simpleDateFormat.parse(endTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //若select的值不为（1或2），则说明是查询所有
        pd = qs.getQuestionByLAP("", "", sd, ed, page - 1, size);
        //给前台传值的+1,因为是页码jpa是从0开始，前台是从1开始
        pd.setCurrent(pd.getCurrent() + 1);
        //当select为1则说明是按照标题查询
        if (select == 1) {
            pd = qs.getQuestionByLAP(keywords, "", sd, ed, page - 1, size);
            pd.setCurrent(pd.getCurrent() + 1);
        } else if (select == 2) {//为2则是按照内容查找
            pd = qs.getQuestionByLAP("", keywords, sd, ed, page - 1, size);
            pd.setCurrent(pd.getCurrent() + 1);
        }
        return pd;
    }


    /**
     * 新增问题
     *
     * @param question
     * @return
     */
    @RequestMapping(value = "insertQuestion", produces = "application/json;charset=utf-8")
    public AjaxResponse insertQuestion(@RequestBody Question question) {
        question.setQuestionDate(new Date());
        // AjaxResponse ajaxResponse = new AjaxResponse();
        if (qs.addQuestion(question) != null) {
            ajaxResponse.setCode(200);
            ajaxResponse.setMsg("新增成功！");
            ajaxResponse.setFlag(true);
            return ajaxResponse;
        } else {
            ajaxResponse.setFlag(false);
            ajaxResponse.setCode(200);
            ajaxResponse.setMsg("新增失败！");
            return ajaxResponse;
        }
    }

    /**
     * 删除问题数据
     *
     * @param questionId
     * @return
     */
    @DeleteMapping(value = "delQuestion/{questionId}", produces = "application/json;charset=utf-8")
    public AjaxResponse deleteQuestion(@PathVariable(value = "questionId") Integer questionId) {
        logger.info("questionId:" + questionId);
        boolean flag = qs.deleteQuestion((long)questionId);
        //  AjaxResponse ajaxResponse = new AjaxResponse();
        if (flag == true) {
            ajaxResponse.setCode(200);
            ajaxResponse.setMsg("删除成功！");
            ajaxResponse.setFlag(true);
            return ajaxResponse;
        } else {
            ajaxResponse.setFlag(false);
            ajaxResponse.setMsg("删除失败！");
            ajaxResponse.setCode(0);
            return ajaxResponse;
        }
    }

    /**
     * 修改问题数据
     *
     * @param question
     * @return
     */
    @RequestMapping(value = "updateQuestion", produces = "application/json;charset=utf-8")
    public AjaxResponse updateQuestion(@RequestBody Question question) {
        question.setQuestionDate(new Date());
        logger.info("question:" + question);
        boolean flag = qs.updateQuestion(question);
        if (flag == true) {
            ajaxResponse.setFlag(flag);
            ajaxResponse.setCode(200);
            ajaxResponse.setMsg("修改成功！");
            return ajaxResponse;
        } else {
            ajaxResponse.setMsg("修改失败！");
            ajaxResponse.setCode(0);
            ajaxResponse.setFlag(flag);
            return ajaxResponse;
        }
    }

    @GetMapping(value = "getQuestionById/{questionId}")
    public Question getQuestionById(@PathVariable(value = "questionId") long questionId) {
        if (questionId == 0) {
            Question question = new Question();
            question.setQuestionId(null);
            question.setQuestionDate(null);
            User user = new User();
            user.setUserId(" ");
            user.setUserName(" ");
            question.setUser(user);
            return question;
        } else {
            return qs.getQuestionById(questionId);
        }
    }

    /**
     * 获取问题标题
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "getQuestionTL")
    public PageData<Question> getQuestionTitleList(@RequestParam(value = "page",defaultValue = "1") int page,
                                                   @RequestParam(value = "size",defaultValue = "8") int size) {
        pd = qs.getQuestionTitleList(page-1, size);
        pd.setCurrent(pd.getCurrent()+1);
        return pd;
    }

}
